import 'dart:math';

import 'package:flutter/material.dart';
import 'package:gridviewdemo/models/models.dart';
import 'package:gridviewdemo/simple/simple.dart';

class NotePlayPage extends StatelessWidget {
  const NotePlayPage({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {

    Puzzle puzzle = _generatePuzzle(3);
    List<Widget> tiles = puzzle.tiles
        .map(
          (tile) => PuzzleTile(
          key: Key('puzzle_tile_${tile.value.toString()}'),
          tile: tile,
        ),
      )
        .toList();

    return Scaffold(
      backgroundColor: const Color.fromARGB(255, 126, 199, 180),
      appBar: AppBar(title: const Text("counter")),
      body: Center(

        child: Container(
            padding: const EdgeInsets.all(46),
          child:  SimplePuzzleBoard(
            key: const Key('simple_puzzle_board_small'),
            size: 3,
            tiles: tiles,
            spacing: 30,
          ),
        )
      ),
    );
  }

  /// Build a randomized, solvable puzzle of the given size.
  Puzzle _generatePuzzle(int size, {bool shuffle = true}) {
    final correctPositions = <Position>[];
    final currentPositions = <Position>[];
    final whitespacePosition = Position(x: size, y: size);

    // Create all possible board positions.
    for (var y = 1; y <= size; y++) {
      for (var x = 1; x <= size; x++) {
//        if (x == size && y == size) {
//          correctPositions.add(whitespacePosition);
//          currentPositions.add(whitespacePosition);
//        } else {

//          if (x * y > 1 ) break;

          final position = Position(x: x, y: y);
          correctPositions.add(position);
          currentPositions.add(position);
//        }
      }
    }

    Random random = Random();

    if (shuffle) {
      // Randomize only the current tile posistions.
      currentPositions.shuffle(random);
    }

    var tiles = _getTileListFromPositions(
      size,
      correctPositions,
      currentPositions,
    );

    var puzzle = Puzzle(tiles: tiles);

    if (shuffle) {
      // Assign the tiles new current positions until the puzzle is solvable and
      // zero tiles are in their correct position.
      while (!puzzle.isSolvable() || puzzle.getNumberOfCorrectTiles() != 0) {
        currentPositions.shuffle(random);
        tiles = _getTileListFromPositions(
          size,
          correctPositions,
          currentPositions,
        );
        puzzle = Puzzle(tiles: tiles);
      }
    }

    return puzzle;
  }

  /// Build a list of tiles - giving each tile their correct position and a
  /// current position.
  List<Tile> _getTileListFromPositions(
      int size,
      List<Position> correctPositions,
      List<Position> currentPositions,
      )
  {
//    final whitespacePosition = Position(x: size, y: size);

    List<Tile> tiles = [];

    for (int i = 1; i <= size * size; i++) {
      int tIndex = i - 1;
//      if (tIndex >= currentPositions.length) break;

      Tile aTile = Tile(
        value: i,
        correctPosition: correctPositions[i - 1],
        currentPosition: currentPositions[i - 1],
      );
      tiles.add(aTile);
    }



//        if (i == size * size)
//          Tile(
//            value: i,
//            correctPosition: whitespacePosition,
//            currentPosition: currentPositions[i - 1],
//            isWhitespace: true,
//          )
//        else
//    Tile(
//      value: i,
//      correctPosition: correctPositions[i - 1],
//      currentPosition: currentPositions[i - 1],
//    )

    return tiles;
  }
}